#include "hwAdc.h"
#include "adc.h"
#include "hxMqtt.h"

static struct
{
    uint8 inited;
    float adcVal;
}rd;

HwAdcResult hwAdcResult;

#define setRdInited(x)       scnsBitSet(rd.inited,0,x)
#define isRdInited()         scnsBitGet(rd.inited,0)

void hwAdcInit(void)
{
    scnsMenuSetPage(8,"hw",NULL);
    scnsMenuAddConstStruct(8,hwAdcResult,val,,SCNS_TYPE_INT16);
    setRdInited(1);
}

void hwAdcInterrupt(void)
{
    if(!isRdInited()){return;}
    ADC_ChannelConfTypeDef ADC1_ChanConf;
    ADC1_ChanConf.Channel=(ADC_CHANNEL_6);
    ADC1_ChanConf.Rank=1;
    ADC1_ChanConf.SamplingTime=ADC_SAMPLETIME_239CYCLES_5;
    HAL_ADC_ConfigChannel(&hadc3,&ADC1_ChanConf);
    HAL_ADC_Start(&hadc3);
    HAL_ADC_PollForConversion(&hadc3,10);
    scnsLowPassFilter(rd.adcVal,HAL_ADC_GetValue(&hadc3),0.9);
    if(rd.adcVal>4000){rd.adcVal=4000;}
    hwAdcResult.val=(100-(rd.adcVal/40));
}
